package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.dao.specially.EnterpriseMchntBaseInfoMapper;
import com.oriental.manage.dao.specially.SpeciallyMchntBaseInfoDeleteMapper;
import com.oriental.manage.model.specially.EnterpriseMchntBaseInfo;
import com.oriental.manage.model.specially.SpeciallyMchntBaseInfoDelete;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.service.specially.BatchMessageProcess;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.model.specially.SpeciallyMchntBaseInfoDeleteDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jinxin on 2017/5/4.
 * 商户删除审核处理
 */
@Service
@Slf4j
public class SpeciallyMchntBaseInfoDeleteProcessImpl  implements BatchMessageProcess<SpeciallyMchntBaseInfoDelete,SpeciallyMchntBaseInfoDeleteDto>{

    @Autowired
   SpeciallyMchntBaseInfoDeleteMapper speciallyMchntBaseInfoDeleteMapper;


    @Override
    public SpeciallyMchntBaseInfoDelete queryDetail(BatchMessage batchMessage) {
        List<String> ids = new ArrayList<>();
        ids.add(batchMessage.getSubmitMsgId());
        List<SpeciallyMchntBaseInfoDelete> speciallyMchntBaseInfoDeleteList= speciallyMchntBaseInfoDeleteMapper.searchSpeciallyMchntBaseInfoDeleteFromIds(ids);
        return speciallyMchntBaseInfoDeleteList==null?null:speciallyMchntBaseInfoDeleteList.get(0);
    }

    @Override
    public List<SpeciallyMchntBaseInfoDeleteDto> query(List<BatchMessage> list) {
        List<String> ids = new ArrayList<>();
        for (BatchMessage b: list
             ) {
            ids.add(b.getSubmitMsgId());
        }
       List<SpeciallyMchntBaseInfoDelete> speciallyMchntBaseInfoDeleteList= speciallyMchntBaseInfoDeleteMapper.searchSpeciallyMchntBaseInfoDeleteFromIds(ids);
        List<SpeciallyMchntBaseInfoDeleteDto> speciallyMchntBaseInfoDeleteDtoList=BeanMapperUtil.mapList(speciallyMchntBaseInfoDeleteList,SpeciallyMchntBaseInfoDeleteDto.class);
        return speciallyMchntBaseInfoDeleteDtoList;
    }

    @Override
    public void auditFail(List<BatchMessage> batchMessage) {
        for (BatchMessage b: batchMessage
             ) {
            SpeciallyMchntBaseInfoDelete s = new SpeciallyMchntBaseInfoDelete();
            s.setAuditFailNote(b.getContent());
            s.setId(b.getSubmitMsgId());
            s.setUploadStatus(MessageStatus.REJECT.getCode());
            speciallyMchntBaseInfoDeleteMapper.updateSpeciallyMchntBaseInfoDelete(s);
        }
    }

    @Autowired
    EnterpriseMchntBaseInfoMapper enterpriseMchntBaseInfoMapper;

    @Override
    public void callback(String status, List<BatchMessage> batchMessage) {

        for (BatchMessage b: batchMessage
                ) {
            SpeciallyMchntBaseInfoDelete s = new SpeciallyMchntBaseInfoDelete();
            s.setId(b.getSubmitMsgId());
            s.setUploadStatus(status);
            speciallyMchntBaseInfoDeleteMapper.updateSpeciallyMchntBaseInfoDeleteForId(s);
            //处理成功，删除企业商户信息
            if(MessageStatus.DEAL_SUCC.getCode().equals(status)){
                List<SpeciallyMchntBaseInfoDelete> speciallyMchntBaseInfoDeleteList=speciallyMchntBaseInfoDeleteMapper.searchSpeciallyMchntBaseInfoDelete(s);
                EnterpriseMchntBaseInfo enterpriseMchntBaseInfo= new EnterpriseMchntBaseInfo();
                if(speciallyMchntBaseInfoDeleteList!=null&&speciallyMchntBaseInfoDeleteList.size()!=0){
                    enterpriseMchntBaseInfo.setCusCode(speciallyMchntBaseInfoDeleteList.get(0).getCusCode());
                    enterpriseMchntBaseInfo.setCenterStatus("02");
                }
                enterpriseMchntBaseInfoMapper.updateEnterpriseMchntBaseInfoForCusCode(enterpriseMchntBaseInfo);
            }
        }
    }
}
